/** 
 * Project Name:ZhiWeiCateringManagementSystem 
 * File Name:DishOrderRecordItemServers.java 
 * Package Name:cn.ccit.servers 
 * Date:2016年11月5日下午8:34:43 
 * Copyright (c) 2016, 120026139@qq.com All Rights Reserved. 
 * 
 */

package cn.ccit.servers;

import java.util.List;
import java.util.Map;

import cn.ccit.DAO.DishOrderRecordDAO;
import cn.ccit.DAO.DishOrderRecordItemDAO;
import cn.ccit.DAOImp.DishOrderRecordDAOimp;
import cn.ccit.DAOImp.DishOrderRecordItemDAOimp;
import cn.ccit.base.BaseConstants;
import cn.ccit.entites.record.CancelDishRecord;
import cn.ccit.entites.record.DishOrderRecord;
import cn.ccit.entites.record.DishOrderRecordItem;
import cn.ccit.iservers.ICancelDishRecordServers;
import cn.ccit.iservers.IDishOrderRecordItemServers;
import cn.ccit.util.ConstantsUtil;

/**
 * ClassName:DishOrderRecordItemServers <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年11月5日 下午8:34:43 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.6
 * @see
 */
public class DishOrderRecordItemServers implements IDishOrderRecordItemServers {
	private DishOrderRecordItemDAO orderItemDao = null;
	private ICancelDishRecordServers cancelServes = null;
	private Map<String, String> map = null;
	private DishOrderRecordDAO iors;

	public DishOrderRecordItemServers() {
		orderItemDao = new DishOrderRecordItemDAOimp();
		cancelServes = new CancelDishRecordServers();
		iors = new DishOrderRecordDAOimp();
	}

	@Override
	public boolean addDishOrderRecordItem(DishOrderRecordItem cdRecord) {
		return orderItemDao.addDishOrderRecordItem(cdRecord);
	}

	@Override
	public boolean removeDishOrderRecordItem(DishOrderRecordItem cdRecord) {
		return orderItemDao.removeDishOrderRecordItem(cdRecord);
	}

	@Override
	public boolean changeDishOrderRecordItemInfo(DishOrderRecordItem cdRecord) {
		return orderItemDao.changeDishOrderRecordItemInfo(cdRecord);
	}

	@Override
	public List<DishOrderRecordItem> getSomeDishOrderRecordItems(int start,
			int limit) {
		return orderItemDao.getSomeDishOrderRecordItems(start, limit);
	}

	@Override
	public DishOrderRecordItem searchDishOrderRecordItemByID(int id) {
		return orderItemDao.searchDishOrderRecordItemByID(id);
	}

	@Override
	public List<DishOrderRecordItem> searchDishOrderRecordItemByYear(String year) {
		return orderItemDao.searchDishOrderRecordItemByYear(year);
	}

	@Override
	public List<DishOrderRecordItem> searchDishOrderRecordItemByMonth(
			String month) {
		return orderItemDao.searchDishOrderRecordItemByMonth(month);
	}

	@Override
	public List<DishOrderRecordItem> searchDishOrderRecordItemByDate(String date) {
		return orderItemDao.searchDishOrderRecordItemByDate(date);
	}

	@Override
	public List<DishOrderRecordItem> searchDishOrderRecordItemBySerialsNumber(
			Long SerialsNumber) {
		return orderItemDao
				.searchDishOrderRecordItemBySerialsNumber(SerialsNumber);
	}

	@Override
	public boolean batchAddDishOrderRecordItem(List<DishOrderRecordItem> list) {
		return orderItemDao.batchAddDishOrderRecordItem(list);
	}

	@Override
	public boolean changeDataEnum(DishOrderRecordItem doiRecord, String reason) {
		if (doiRecord.getEnumCode() == BaseConstants.DishState.RetreatFood
				.ordinal()) {
			CancelDishRecord cdr =null; 
			/**
			 * 当进行退菜时，修改总记录的总价
			 */
			
				DishOrderRecordItem temp = orderItemDao
						.searchDishOrderRecordItemByID(doiRecord.getId());
				List<DishOrderRecord> l = iors
						.searchDishOrderRecordBySerialsNumber(temp
								.getSerialsNumber());
				DishOrderRecord record = l.get(0);
				record.setPrice(record.getPrice() - temp.getPrice());
				iors.changeDishOrderRecordInfo(record);
				cdr = this.setCancelOrderData(
						new CancelDishRecord(), temp, reason);
				cancelServes.addCancelDishRecord(cdr);
			
		}
		return orderItemDao.changeDataEnum(doiRecord);
	}

	private CancelDishRecord setCancelOrderData(CancelDishRecord cdRecord,
			DishOrderRecordItem doiRecord, String reason) {
		map = ConstantsUtil.getNowTimeMap();
		cdRecord.setDishId(doiRecord.getDishId());
		cdRecord.setDishName(doiRecord.getDishName());
		cdRecord.setPrice(doiRecord.getPrice());
		cdRecord.setReason(reason);
		cdRecord.setTid(doiRecord.getTid());
		cdRecord.setTname(doiRecord.getTname());
		cdRecord.setSerialsNumber(doiRecord.getSerialsNumber());
		cdRecord.setCreatTime(map.get(BaseConstants.CRTEAT_TIME));
		cdRecord.setOrderDate(map.get(BaseConstants.DATE));
		cdRecord.setOrderMonth(map.get(BaseConstants.MONTH));
		cdRecord.setOrderYear(map.get(BaseConstants.YEAR));
		cdRecord.setLastModfiyTime(map.get(BaseConstants.CRTEAT_TIME));
		cdRecord.setLastModfiyUid(doiRecord.getCreatUid());
		cdRecord.setLastModfiyUname(doiRecord.getCreatUname());
		cdRecord.setName("退菜记录");
		return cdRecord;
	}
}
